package com.gxuwz.business.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.management.relation.Role;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.gxuwz.business.service.ISysRoleRightService;
import com.gxuwz.business.service.ISysRoleService;
import com.gxuwz.business.dao.impl.SysRoleDAO;
import com.gxuwz.business.entity.SysRole;
import com.gxuwz.business.entity.SysRoleRight;

/**
 * <p>Title: 类名：角色-业务逻辑--实现</p>
 * <p>Description:mailsender-业务逻辑/n</p>
 * @author:	梧州学院 软件开发中心 
 * @date:2015.07.31
 */

@Service("sysRoleService")
public class SysRoleServiceImpl implements ISysRoleService {


    @Autowired ISysRoleRightService sysRoleRightService;	

	@Autowired
	private SysRoleDAO sysRoleDAO;


	/**
	 * 添加角色
	 * @param sysRole
	 */
	public void add(SysRole sysRole, String[] rightId) {
		for (int i = 0; i < rightId.length; i++) {
			SysRoleRight sysRoleRight = new SysRoleRight();
			sysRoleRight.setRoleId(sysRole.getRoleId());
			sysRoleRight.setRightId(rightId[i]);
			sysRoleRightService.addRoleRight(sysRoleRight);
		}
		sysRoleDAO.save(sysRole);
	}


	public void delete(SysRole sysRole){
		
		 sysRoleDAO.delete(sysRole);
		
		
	}


	
	@Override
	public void update(SysRole sysRole) {
	
		sysRoleDAO.update(sysRole);
	}


	@Override
	public SysRole findById(String roleId) {
		// TODO Auto-generated method stub
		return sysRoleDAO.get(SysRole.class, roleId);
	}


	@Override
	public List<SysRole> getRoleAll() {
		// TODO Auto-generated method stub
		return sysRoleDAO.getAll(SysRole.class);
	}


	@Override
	public void update(SysRole sysRole, String[] rightId) {
		        //根据角色编号删除角色权限
				sysRoleRightService.delete(sysRole.getRoleId());
				//添加角色权限
				for(int i = 0; i < rightId.length; i++){
					SysRoleRight sysRoleRight = new SysRoleRight();
					sysRoleRight.setRoleId(sysRole.getRoleId());
					sysRoleRight.setRightId(rightId[i]);
					sysRoleRightService.addRoleRight(sysRoleRight);
				}
				//更新角色信息
				sysRoleDAO.update(sysRole);
	}


	@Override
	public List<String> findRightByRoleId(String roleId) {
		return sysRoleDAO.findRightByRoleId(roleId);
	}
}
